package cn.interestingshop.web.manager;

import cn.interestingshop.entity.User;
import cn.interestingshop.service.user.UserService;
import cn.interestingshop.service.user.UserServiceImpl;
import cn.interestingshop.utils.EmptyUtils;
import cn.interestingshop.utils.Pager;
import cn.interestingshop.web.AbstractServlet;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "UserServlet",urlPatterns = "/manager/user")
public class UserServlet extends AbstractServlet {

    @Override
    public Class getServletClass() {
        return UserServlet.class;
    }

    private UserService userService = new UserServiceImpl();

    /**
     * 查询用户列表
     * @param request
     * @param response
     * @return
     */
    public String queryUserList(HttpServletRequest request,HttpServletResponse response){
        //调用查询用户列表方法
        //获取当前页
        String currentPage = request.getParameter("currentPage");
        int currentPageNo = EmptyUtils.isEmpty(currentPage)?1:Integer.valueOf(currentPage);
        //获取用户列表
        List<User> allUserList = userService.findAllUserList(currentPageNo, 5);
        //获取总记录条数
        int count = userService.findUserCount();
        Pager pager = new Pager(count,5,currentPageNo);
        pager.setUrl("manager/user?action=queryUserList");
        //将用户列表集合放入request作用域中
        request.setAttribute("userList", allUserList);
        //将pager对象放入request作用域
        request.setAttribute("pager", pager);
        return "/manager/user/userList";
    }


    public String toUpdateUser(HttpServletRequest request,HttpServletResponse response){



        UserService userService=new UserServiceImpl();


        User user=userService.selectOne(Integer.valueOf(request.getParameter("id")));

        request.setAttribute("user",user);

        return "/manager/user/toUpdateUser";
    }



    public void updateUser(HttpServletRequest request, HttpServletResponse response) {

        response.setContentType("text/html;charset=utf-8");

        int update=0;
        Boolean account=false;
        Boolean phone=false;

        if(!EmptyUtils.isEmpty(request.getParameter("id"))){



            User user = new User();

            user.setId(Integer.valueOf(request.getParameter("id")));
            user.setAccount(request.getParameter("account"));
            user.setEmail(request.getParameter("email"));
            user.setIdCardNo(request.getParameter("idCardNo"));
            user.setNickName(request.getParameter("nickName"));
            user.setPhone(request.getParameter("phone"));
            user.setType(Integer.valueOf(request.getParameter("type")));

            UserService userService = new UserServiceImpl();


            update= userService.update(user);







        }else{
            User user = new User();

            user.setAccount(request.getParameter("account"));
            user.setEmail(request.getParameter("email"));
            user.setIdCardNo(request.getParameter("idCardNo"));
            user.setNickName(request.getParameter("nickName"));
            user.setPassword(request.getParameter("password"));
            user.setPhone(request.getParameter("phone"));
            user.setType(Integer.valueOf(request.getParameter("type")));

            UserService userService = new UserServiceImpl();

            account=userService.selectOneAccount(request.getParameter("account"));
            phone=userService.selectOnePhone(request.getParameter("phone"));

            if(account==false && phone==false) {

                update = userService.update(user);
            }
        }

        JSONObject jsonObject=new JSONObject();



        if (update > 0) {
            jsonObject.put("status",1);
            if(!EmptyUtils.isEmpty(request.getParameter("id"))){
                jsonObject.put("message","修改成功!");
            }else{
                jsonObject.put("message","添加成功!");
            }

        } else {
            jsonObject.put("status",0);
            if(!EmptyUtils.isEmpty(request.getParameter("id"))){
                jsonObject.put("message","修改失败!");
            }else{
                if(account==false && phone==false){
                    jsonObject.put("message","添加失败!");
                }else if(account==true){
                    jsonObject.put("message","姓名重复!");
                }else if(phone==true){
                    jsonObject.put("message","电话重复!");
                }

            }

        }

        try {
            response.getWriter().print(jsonObject);
        } catch (IOException e) {
            e.printStackTrace();
        }



    }

    public String toAddUser(HttpServletRequest request,HttpServletResponse response){
        return "/manager/user/toUpdateUser";
    }

    public void deleteUserById(HttpServletRequest httpServletRequest,HttpServletResponse response){
        response.setContentType("text/html;charset=utf-8");

        int delete=0;



        UserService service=new UserServiceImpl();

        delete = service.delete(Integer.valueOf(httpServletRequest.getParameter("id")));


        JSONObject jsonObject=new JSONObject();



        if (delete> 0) {
            jsonObject.put("status",1);
            jsonObject.put("message","删除成功!");

        } else {
            jsonObject.put("status",0);
            jsonObject.put("message","删除失败!");
        }
            try {
                response.getWriter().print(jsonObject);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public String index(HttpServletRequest request,HttpServletResponse response){
            return "/manager/user/userInfo";
        }


}
